package br.com.musclebook.bean;

import java.io.Serializable;
import java.util.Calendar;
import java.util.ResourceBundle;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

import org.primefaces.event.SelectEvent;
import org.primefaces.model.DefaultScheduleEvent;
import org.primefaces.model.DefaultScheduleModel;
import org.primefaces.model.ScheduleEvent;
import org.primefaces.model.ScheduleModel;

import br.com.musclebook.entity.RoutineDay;
import br.com.musclebook.entity.RoutineDayTraining;
import br.com.musclebook.entity.User;
import br.com.musclebook.entity.UserCurrentRoutine;
import br.com.musclebook.service.RoutineService;
import br.com.musclebook.service.UserService;

@Named
@ViewScoped
public class ScheduleBean implements Serializable {

    private static final long serialVersionUID = 1L;

    @Inject
    private SessionInfoBean sessionInfoBean;

    @Inject
    private UserService userService;
    
    @Inject
    private RoutineService routineService;

    private ScheduleModel scheduleModel;

    private ScheduleEvent event;

    @PostConstruct
    public void init() {
	User user = this.userService.find(this.sessionInfoBean.getUserId());
	this.scheduleModel = new DefaultScheduleModel();
	this.event = new DefaultScheduleEvent();

	UserCurrentRoutine currentRoutine = user.getCurrentRoutine();

	if (currentRoutine != null) {

	    Calendar dtInicio = Calendar.getInstance();
	    dtInicio.setTime(currentRoutine.getStart());

	    Calendar dtFim = Calendar.getInstance();
	    dtFim.setTime(currentRoutine.getStart());
	    dtFim.add(Calendar.DATE, 7 * currentRoutine.getRoutine().getDurationInWeeks());
	    
	    currentRoutine.setRoutine(routineService.find(currentRoutine.getRoutine().getId()));

	    ResourceBundle bundle = ResourceBundle.getBundle("resources.MusclebookMessages", FacesContext.getCurrentInstance().getViewRoot().getLocale());
	    
	    while (dtInicio.before(dtFim)) {
		for (RoutineDay rDay : currentRoutine.getRoutine().getRoutineDays()) {
		    if (rDay.getDayOfWeek().equals(dtInicio.get(Calendar.DAY_OF_WEEK))) {
			for (RoutineDayTraining training : rDay.getRoutineDayTrainings()) {
			    DefaultScheduleEvent trainingEvent = new DefaultScheduleEvent(bundle.getString(training.getExerciseCategory().getLabel()), dtInicio.getTime(),  dtInicio.getTime(), true);
			    trainingEvent.setData(training);
			    trainingEvent.setStyleClass(training.getExerciseCategory().getScheduleEventClass());
			    this.scheduleModel.addEvent(trainingEvent);
			}
		    }
		}

		dtInicio.add(Calendar.DATE, 1);
	    }
	}
    }

    public void onEventSelect(SelectEvent selectEvent) {
	event = (ScheduleEvent) selectEvent.getObject();
    }

    public ScheduleModel getScheduleModel() {
	return scheduleModel;
    }

    public void setScheduleModel(ScheduleModel scheduleModel) {
	this.scheduleModel = scheduleModel;
    }

    public ScheduleEvent getEvent() {
	return event;
    }

    public void setEvent(ScheduleEvent event) {
	this.event = event;
    }

}
